package cn.fsw.pethome.base.controller;

import cn.fsw.pethome.base.query.Page;
import cn.fsw.pethome.base.service.IBaseService;
import cn.fsw.pethome.org.util.AjaxResult;
import cn.fsw.pethome.org.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T> {
    @Autowired
    private IBaseService<T> baseService;

    @GetMapping("/list")
    @ApiOperation(value = "查询全部部门信息" )
    public List<T> selectAll(){
        return baseService.selectAll();
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "高级查询和分页" )
    public PageList<T> pageSelevtAll(@RequestBody Page query){

        return baseService.pageSelevtAll(query);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public T selectById(@PathVariable("id") Long id){
        return baseService.selectById(id);
    }

    @PostMapping
    @ApiOperation(value = "新增或者修改部门信息" )
    public AjaxResult add(@RequestBody T t){
        try {
            baseService.add(t);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    @PutMapping
    @ApiOperation(value = "新增或者修改部门信息" )
    public AjaxResult update(@RequestBody T t){
        try {
                baseService.update(t);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            baseService.delete(id);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
